#pragma once

#include <string>
#include <vector>

namespace liba {

template<typename Sym>
class Tokenizer 
{
public:
	Tokenizer()
		: delimiter(0)
	{ }

	void SetDelimiter(Sym value) { this->delimiter = value; }

	std::vector<std::basic_string<Sym> > Tokenize(const std::basic_string<Sym> str) const
	{
		std::vector<std::basic_string<Sym> > result;

		std::vector<Sym> buffer;
		buffer.resize(str.size() + 1);
		Sym* bufferStart = &buffer[0];
		Sym* currentPos = bufferStart;
	
		for (typename std::basic_string<Sym>::const_iterator i = str.begin(); i != str.end(); ++i)
		{
			if (*i == delimiter)
			{
				*currentPos++ = ((Sym)0);
				result.push_back(std::basic_string<Sym>(bufferStart));
				currentPos = bufferStart;
			}
			else
			{
				*currentPos++ = *i;
			}
		}
	
		if (currentPos != bufferStart)
		{
			*currentPos++ = ((Sym)0);
			result.push_back(std::basic_string<Sym>(bufferStart));
		}
	
		return result;
	}
private:
	Sym delimiter;
};

} // namespace liba